package com.lhd.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.lhd.component.RedisComponent;
import com.lhd.entity.constants.Constants;
import com.lhd.entity.dto.AdditionInfoDto;
import com.lhd.entity.dto.PostActivityDto;
import com.lhd.entity.dto.UserDto;
import com.lhd.entity.enums.ActivityTypeRedisKeyEnum;
import com.lhd.entity.enums.PageSize;
import com.lhd.entity.enums.ResponseCodeEnum;
import com.lhd.entity.po.Activity;
import com.lhd.entity.po.User;
import com.lhd.entity.query.ActivityQuery;
import com.lhd.entity.query.UserQuery;
import com.lhd.entity.vo.PaginationResultVO;
import com.lhd.entity.vo.ResponseVO;
import com.lhd.exception.BusinessException;
import com.lhd.service.ActivityService;
import com.lhd.service.UserService;
import com.lhd.service.qrcode.RefreshCodeService;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.*;


import static com.lhd.entity.constants.Constants.REDIS_KEY;

/**
 * @Author: liuhd
 * @Date: 2025/03/10
 * @Description: Activity控制器
 */
@RestController
@RequestMapping("/activity")
public class ActivityController extends ABaseController {

    @Resource
    private ActivityService activityService;
    @Resource
    private UserService userService;
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private RefreshCodeService refreshCodeService;
    /**
     * @param postActivityDto
     * @param type
     * @return com.lhd.entity.vo.ResponseVO
     * @description: 发布签到
     * @author liuhd
     * 2025/4/30 11:09
     */
    @PostMapping("/{type}")
    public ResponseVO postActivity(@RequestBody PostActivityDto postActivityDto, @PathVariable String type) {
        UserDto userInfoToken = getUserInfoToken();
        List<Activity> activityList = activityService.postActivity(postActivityDto, userInfoToken.getId(), type);
        return getSuccessResponseVO(activityList);
    }

    /**
     * @param
     * @return com.lhd.entity.vo.ResponseVO
     * @description: 获取老师发布的签到
     * @author liuhd
     * 2025/4/30 11:09
     */

    @GetMapping("/list")
    public ResponseVO getActivityList(@RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Integer pageNo) {
        Integer teacherId = getUserInfoToken().getId();
        Integer role = userService.getUserById(teacherId).getRole();
        if (role == 0) {
            throw new BusinessException("身份应该为老师");
        }
        if (pageNo == null || pageNo == 0) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = PageSize.SIZE15.getSize();
        }
        ActivityQuery activityQuery = new ActivityQuery();
        activityQuery.setPageNo(Long.valueOf(pageNo));
        activityQuery.setPageSize(Long.valueOf(pageSize));
        activityQuery.setUid(teacherId);
        activityQuery.setGetActivityList(true);

        PaginationResultVO<Activity> activityPaginationResultVO = activityService.findListByPage(activityQuery);

        return getSuccessResponseVO(activityPaginationResultVO);
    }

    /**
     * @description: 开启刷新二维码事件
     * @param aidList 涉及活动
     * @param refreshTime 刷新间隔
     * @return com.lhd.entity.vo.ResponseVO
     * @author liuhd
     * 2025/5/11 11:58
     */
    @GetMapping("/openRefreshCodeEvent")
    public ResponseVO openRefreshCodeEvent(@RequestParam("aidList") List<Integer> aidList, @RequestParam("refreshTime") Integer refreshTime) {
        refreshCodeService.stopRefreshTask(aidList);
        refreshCodeService.handleRefreshCodeTask(aidList, refreshTime);
        return getSuccessResponseVO(null);
    }




    /**
     * @param aid
     * @return com.lhd.entity.vo.ResponseVO
     * @description: 停止活动
     * @author liuhd
     * 2025/5/9 21:29
     */
    @GetMapping("/quit/{aid}")
    public ResponseVO quit(@PathVariable("aid") Integer aid) {
        activityService.quit(aid);
        return getSuccessResponseVO(null);
    }


    @GetMapping("/additionInfo/{aid}/{type}")
    public ResponseVO getAdditionInfo(@PathVariable("aid") Integer aid, @PathVariable("type") String type) {
        String key = REDIS_KEY + getRedisKey(type) + ":" + aid;
        Map commonMap = redisComponent.getCommonMap(key);
		AdditionInfoDto additionInfoDto = new AdditionInfoDto();
		BeanUtil.fillBeanWithMap(commonMap,additionInfoDto,false);
		System.out.println(additionInfoDto);

		return getSuccessResponseVO(additionInfoDto);
    }


    private static String getRedisKey(String type) {
        String redisKey = null;
        if (ActivityTypeRedisKeyEnum.GESTURE_CHECK_IN.getType().equals(type)) {
            redisKey = ActivityTypeRedisKeyEnum.GESTURE_CHECK_IN.getRedisKey();
        }
        if (ActivityTypeRedisKeyEnum.LOCATION_CHECK_IN.getType().equals(type)) {
            redisKey = ActivityTypeRedisKeyEnum.LOCATION_CHECK_IN.getRedisKey();
        }
        if (ActivityTypeRedisKeyEnum.QR_CODE_CHECK_IN.getType().equals(type)) {
            redisKey = ActivityTypeRedisKeyEnum.QR_CODE_CHECK_IN.getRedisKey();
        }
        if (ActivityTypeRedisKeyEnum.CODE_CHECK_IN.getType().equals(type)) {
            redisKey = ActivityTypeRedisKeyEnum.CODE_CHECK_IN.getRedisKey();
        }
        return redisKey;
    }

}